package com.lbssoft.gpsbd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbssoft.datalink.model.Command;
import com.lbssoft.gpsbd.dao.CommandRecordMapper;
import com.lbssoft.gpsbd.model.CommandRecord;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.service.CommandRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nczc on 2018/7/27.
 */
@Service
public class CommandRecordServiceImpl implements CommandRecordService {

    @Autowired
    CommandRecordMapper commandRecordMapper;

    @Override
    public void handlerCommandResult(Command command) {
       int id =  commandRecordMapper.getRecordId(command.getDeviceId(),command.getSn());
        CommandRecord record = new CommandRecord();
        record.setId(id);
        record.setRes(command.getRes());
        if(command.getResult()==0){
            record.setStatus(0);
        }else if(command.getResult()==1){
            record.setStatus(3);
        }
        commandRecordMapper.update(record);
    }


    @Override
    public Result list(long deviceId,int page,int pageSize) {
        Result result = new Result();
        PageHelper.startPage(page, pageSize);
        List<CommandRecord> list = commandRecordMapper.getList(deviceId);
        PageInfo pageInfo = new PageInfo(list);
        Page pages = (Page) list;
        Map data = new HashMap();
        data.put("page", page);
        data.put("total", pageInfo.getTotal());
        data.put("list", pages);
        result.setData(data);
        return result;
    }
}
